#!/bin/bash

set -x

# We start by adding extra apt packages, since pip modules may required library
if [ "$EXTRA_APT_PACKAGES" ]; then
    echo "EXTRA_APT_PACKAGES environment variable found.  Installing."
    apt update -y
    apt install -y $EXTRA_APT_PACKAGES
fi

if [ -e "/opt/app/environment.yml" ]; then
    echo "environment.yml found. Installing packages"
    /opt/conda/bin/conda env update -f /opt/app/environment.yml
else
    echo "no environment.yml"
fi

if [ "$EXTRA_CONDA_PACKAGES" ]; then
    echo "EXTRA_CONDA_PACKAGES environment variable found.  Installing."
    /opt/conda/bin/conda install $EXTRA_CONDA_PACKAGES
fi

if [ "$EXTRA_PIP_PACKAGES" ]; then
    echo "EXTRA_PIP_PACKAGES environment variable found.  Installing".
    /opt/conda/bin/pip install $EXTRA_PIP_PACKAGES
fi

# Activate the specified or default conda environment
#conda_env="rapids"
#if [ "$CONDA_ENV" ]; then
#    conda_env="$CONDA_ENV"
#fi
source activate $CONDA_ENV

# Run pre-commands
if [ "$PRE_RUN_HOOK" ]; then
    ${PRE_RUN_HOOK}
fi

# Run extra commands
"$@"

# Run POST commands
if [ "$POST_RUN_HOOK" ]; then
    ${POST_RUN_HOOK}
fi
